//
//  MSMeController.m
//  觅尚
//
//  Created by No.8 on 2019/3/19.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSMeController.h"
#import "MSHomeMeCell.h"
#import "MSMyModel.h"
#import "MSPersonalHomepageController.h"
#import "MSWalletController.h"
#import "MSNoticeMessegeController.h"
#import "MSQRCodeController.h"
#import "MSSetUpController.h"
#import "MSBeverageStorageController.h"
#import "MSMyManagementController.h"
#import "MSEmployeeAuditController.h"
#import "MSBuidingIncorporationController.h"
#import "MSSeeAttendanceStatisticsController.h"
#import "MSAlbumModel.h"
#import "MSRoomTakeNotesController.h"

@interface MSMeController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 数组*/
@property(nonatomic,strong)NSArray *MyTableViewTitleArray;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *MyModelArray;


/** 模型*/
@property(nonatomic,strong)MSMyModel * model;

/** 图片*/
@property(nonatomic,strong)UIImageView *photoIMG;

/** 姓名*/
@property(nonatomic,strong)UILabel *NameLabel;

/** 职位*/
@property(nonatomic,strong)UILabel *positionLabel;

/** 职位*/
@property(nonatomic,strong)NSMutableArray *albumListArray;

@end

@implementation MSMeController
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.albumListArray = [NSMutableArray new];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self relodata];
        
        [self Relodata];
        
    });
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.MyModelArray = [NSMutableArray new];
    self.MyTableViewTitleArray = [NSMutableArray new];
    self.view.backgroundColor = backgroundcolor;
    
    
    [self CreattableView];
    
    [self CreatView];
    
    
    
}
- (void)Relodata{
    [self.albumListArray removeAllObjects];
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid]};
    
    [HTTPRequest POST:MSGetUserInfo params:params succeed:^(id responseObject) {
         WYCLog(@"-------%@",responseObject);
        if (CodeStr) {
            MSAlbumModel * model = [[MSAlbumModel alloc] init];
            model.ID = @"00";
             [self.albumListArray addObject:model];
            
            for (NSDictionary * dict in responseObject[@"data"][@"albumList"]) {
                MSAlbumModel * model = [MSAlbumModel mj_objectWithKeyValues:dict];
                [self.albumListArray addObject:model];
            }
            
//            NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
            // 排序结果
           // self.albumListArray = [self.albumListArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            
            [self.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",responseObject[@"data"][@"user"][@"avatarUrl"]]] placeholderImage:[UIImage imageNamed:@"我的占位头像"]];

            self.NameLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"user"][@"userName"]];

            self.positionLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"user"][@"roleName"]];

        }else{

            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];

    
}
- (void)relodata{
    [self.MyModelArray removeAllObjects];
    NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAarray"];
    WYCLog(@"-------%@",arr);
    for (NSDictionary * dict in arr) {
        MSMyModel * model = [MSMyModel mj_objectWithKeyValues:dict];
        if ([model.pma_isShow isEqualToString:@"0"]) {
            [self.MyModelArray addObject:model];
        }
        
    }
        MSMyModel * model = [[MSMyModel alloc] init];
        model.bay_name = @"二维码绑定";
        model.key = @"code_key";
        model.bem_id = @"3";
        model.pma_isShow = @"0";
        [self.MyModelArray addObject:model];
    
    
    
        MSMyModel * model1 = [[MSMyModel alloc] init];
        model1.bay_name = @"设置";
        model1.key = @"set_key";
        model1.bem_id = @"3";
        model1.pma_isShow = @"0";
        [self.MyModelArray addObject:model1];
        [_tableView reloadData];
    
    
}
- (void)CreatView{
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 800 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    
    UIView * ImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 400 * px)];
    UIImage*image = [UIImage imageNamed:@"hdzx_bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [HeaderView addSubview:ImageView];
    
    /** title*/
    UILabel * Navtitle = [[UILabel alloc] init];
    if ([WRNavigationBar isIphoneX]) {
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 55, 300 * px, 60 * px);
    }else{
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 30, 300 * px, 60 * px);
        
    }
    Navtitle.textColor = [UIColor whiteColor];
    Navtitle.text = @"我的";
    Navtitle.textAlignment = NSTextAlignmentCenter;
    Navtitle.font = [UIFont boldSystemFontOfSize:SYRealValue(38 / 2)];
    [HeaderView addSubview:Navtitle];
    
    //头像
    UIImageView * photoIMG = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 - 150 * px, CGRectGetMaxY(ImageView.frame) - 60 * px, 300 * px, 300 * px)];
    photoIMG.layer.cornerRadius = 150 * px;
    photoIMG.layer.masksToBounds = YES;
    photoIMG.image = [UIImage imageNamed:@"上传头像"];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage)];
    [photoIMG addGestureRecognizer:tapGesture];
    photoIMG.userInteractionEnabled = YES;
    [HeaderView addSubview:photoIMG];
    self.photoIMG = photoIMG;
    
    
    /** 昵称*/
    UILabel * NameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(photoIMG.frame) + 30 * px, ScreenWidth, 60 * px)];
    NameLabel.textColor = [UIColor whiteColor];
    NameLabel.text = @"王五";
    NameLabel.textAlignment = NSTextAlignmentCenter;
    NameLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(38 / 2)];
    [HeaderView addSubview:NameLabel];
    self.NameLabel = NameLabel;
    
    
    /** 职位*/
    UILabel * positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(NameLabel.x, CGRectGetMaxY(NameLabel.frame) + 20 * px, NameLabel.width, 60 * px)];
    positionLabel.textColor = [UIColor whiteColor];
    positionLabel.text = @"总经理";
    positionLabel.textAlignment = NSTextAlignmentCenter;
    positionLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [HeaderView addSubview:positionLabel];
    self.positionLabel = positionLabel;
    
    self.tableView.tableHeaderView = HeaderView;
    
    
    
    
}
- (void)clickImage{
    MSPersonalHomepageController * PersonalVC = [[MSPersonalHomepageController alloc] init];
    PersonalVC.albumList = self.albumListArray;
    [self.navigationController pushViewController:PersonalVC animated:NO];
    
}
/** 表视图*/
- (void)CreattableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    //    if (!_tableView) {
    
    if (@available(iOS 11.0, *)) {
        
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    //    }
    //    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.MyModelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSHomeMeCell * cell = [MSHomeMeCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.MyModel = self.MyModelArray[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{       MSMyModel * model = self.MyModelArray[indexPath.row];
    if ([model.key isEqualToString:@"my_companymanage"]){
        
        //            公司管理
        MSMyManagementController * MyManagementVC = [[MSMyManagementController alloc] init];
        [self.navigationController pushViewController:MyManagementVC animated:NO];
        
    }else if ([model.key isEqualToString:@"set_key"]){
        //设置
        MSSetUpController * setVC = [[MSSetUpController alloc] init];
        [self.navigationController pushViewController:setVC animated:NO];
    }
    else if ([model.key isEqualToString:@"code_key"]) {
        
        //二维码绑定
        MSQRCodeController * QRCodeVC = [[MSQRCodeController alloc] init];
        [self.navigationController pushViewController:QRCodeVC animated:NO];
    }else if ([model.key isEqualToString:@"my_attendance"]) {
        
        //出勤人员列表
        MSSeeAttendanceStatisticsController * QRCodeVC = [[MSSeeAttendanceStatisticsController alloc] init];
        [self.navigationController pushViewController:QRCodeVC animated:NO];
    }else if ([model.key isEqualToString:@"room_preservedwine"]) {
        
        //酒水存取
        MSBeverageStorageController * BeverageStorageVC = [[MSBeverageStorageController alloc] init];
        [self.navigationController pushViewController:BeverageStorageVC animated:NO];
    }else if ([model.key isEqualToString:@"my_evaluate"]) {
        
        //评价收益
        MSWalletController * WalletVC = [[MSWalletController alloc] init];
        [self.navigationController pushViewController:WalletVC animated:NO];
    }else if ([model.key isEqualToString:@"my_notice"]) {
        
        //通知
        MSNoticeMessegeController * WalletVC = [[MSNoticeMessegeController alloc] init];
        [self.navigationController pushViewController:WalletVC animated:NO];
    }else if ([model.key isEqualToString:@"room_history"]) {
        
        //房间服务历史记录
        MSRoomTakeNotesController * WalletVC = [[MSRoomTakeNotesController alloc] init];
        [self.navigationController pushViewController:WalletVC animated:NO];
    }else{
        //            员工管理
        MSEmployeeAuditController * EmployeeAuditVC = [[MSEmployeeAuditController alloc] init];
        [self.navigationController pushViewController:EmployeeAuditVC animated:NO];
    }
    
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"我的";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    naviagtionBar.leftBtn.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
}
@end
